#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/timer.h>
static unsigned int GPIO0_B7 = 15; //这里需要读者自己改
static unsigned int last_state =0;
 
struct timer_list mytimer;
 
void mytimer_function(struct timer_list *timer){
 
    if(gpio_get_value(GPIO0_B7)!=last_state)
        {
            last_state=gpio_get_value(GPIO0_B7);
            printk("lsdeg_ops_detect state: %d\n", last_state);
        }
 
}
 
 
static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
{
 
    mod_timer(&mytimer, jiffies + 1); //10ms
    return IRQ_HANDLED;
}
 
 
static int __init gpio_interrupt_init(void)
{
 
    mytimer.expires = jiffies + 1;  //10ms
    timer_setup(&mytimer, mytimer_function, 0);
 
    add_timer(&mytimer);
 
    if (gpio_request(GPIO0_B7, "GPIO0_B7") < 0) {
        printk("lsdeg_Failed to request GPIO\n");
        return -ENODEV;
    }
 
    gpio_direction_input(GPIO0_B7);
 
    if (request_irq(gpio_to_irq(GPIO0_B7), gpio_irq_handler, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
                    "gpio_interrupt", NULL) < 0) {
        printk("lsdeg_Failed to request interrupt\n");
        return -ENODEV;
    }
 
    printk("lsdeg_ops_detect_GPIO interrupt driver initialized\n");
 
    return 0;
}
 
static void __exit gpio_interrupt_exit(void)
{
    free_irq(gpio_to_irq(GPIO0_B7), NULL);
    gpio_free(GPIO0_B7);
    del_timer(&mytimer);
    printk("lsdeg_GPIO interrupt driver exit\n");
}
 
module_init(gpio_interrupt_init);
module_exit(gpio_interrupt_exit);
 
MODULE_LICENSE("GPL");
MODULE_AUTHOR("handsome");
MODULE_DESCRIPTION("GPIO Interrupt Driver");